package archer.framework.common.support.exception.view;

import archer.framework.utils.JSONUtils;
import org.springframework.web.servlet.view.AbstractView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Map;

/**
 * 异常时返回json的ModelAndView
 *
 * @author kent
 * @date 16/6/16.
 */
public class ExceptionJsonView extends AbstractView {

    public static final String EXECUTE_RESULT = "exceptionResult";

    private static final String CONTENT_TYPE = "application/json";

    @Override
    protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType(CONTENT_TYPE);
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        OutputStream stream = response.getOutputStream();

        stream.write(JSONUtils.serialize(model.get(EXECUTE_RESULT)).getBytes());
    }
}
